﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility1;

namespace eDream.Page
{
    public partial class register : System.Web.UI.UserControl
    {
        public string pathUrl = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            pathUrl = WebUtils.pathUrl();
            if (!IsPostBack)
            {
                for (int i = 2010; i >= 1950; i--)
                {
                    cbNam.Items.Add(i.ToString(), i);
                }

                cbNgay.SelectedIndex = 0;
                cbThang.SelectedIndex = 0;
                cbNam.SelectedIndex = 30;
                cbGioitinh.SelectedIndex = 0;
            }
        }


        int timNguoi(String uName)
        {
            int id = 0;
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.UserDBs
                           where k.UserName == uName
                           select k;

                foreach (UserDB u in list)
                {
                    id = u.ID;

                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
            return id;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int id = timNguoi(txtUserName.Text.Trim());
            DataClasses1DataContext dx = new DataClasses1DataContext();

            if (id == 0)
            {
                if (txtPass1.Text != "" && txtLassname.Text != "" && txtFirstname.Text != "" && txtEmail.Text != "" && txtDienthoai.Text != "" && txtDiachi.Text != "" && txtUserName.Text != "" && txtUserName.Text.Length > 4)
                {
                    string chudau = "";
                    if (txtUserName.Text.Length == 5)
                        chudau = txtUserName.Text.Substring(0, 1);
                    if (chudau != "A" && chudau != "a")
                        try
                        {

                            dx.Connection.Open();
                            UserDB u = new UserDB();
                            u.address = txtDiachi.Text;
                            u.FirtName = txtFirstname.Text;
                            u.Fullname = txtLassname.Text + " " + txtFirstname.Text;
                            u.Lastname = txtLassname.Text;
                            u.gioitinh = cbGioitinh.Text;
                            u.flag = 1;
                            u.ngaydangky = DateTime.Now;
                            u.ngaysinh = int.Parse(cbNgay.Value.ToString());
                            u.thangsinh = int.Parse(cbThang.Value.ToString());
                            u.namsinh = int.Parse(cbNam.Value.ToString());
                            u.Password = Global.ToEncoding(txtPass1.Text.Trim());
                            u.parentID = 0;
                            u.Office_ID = 0;
                            u.visible = true;
                            u.UserIDTuvan = 0;
                            u.UserName = txtUserName.Text.Trim();
                            u.email = txtEmail.Text;
                            u.phone = txtDienthoai.Text;
                            u.q = 0;

                            dx.UserDBs.InsertOnSubmit(u);
                            dx.SubmitChanges();

                            // p1.Visible = false;
                            //p2.Visible = true;
                            login();
                            txtDiachi.Text = "";
                            txtDienthoai.Text = "";
                            txtEmail.Text = "";
                            txtFirstname.Text = "";
                            txtLassname.Text = "";
                            txtPass1.Text = "";

                            txtDiachi.Text = "";
                            txtUserName.Text = "";

                            lbthongbao.Text = "Chúc mừng bạn đã đăng ký thành công";
                        }
                        catch (Exception) { lbthongbao.Text = "Vui lòng thử lại sau"; }
                        finally { dx.Connection.Close(); }
                    else
                    {
                        lbthongbao.Text = "Tên đăng nhập đã tồn tại";
                        lbthongbao.ForeColor = Color.Red;
                    }
                }
            }
            else
            {
                lbthongbao.Text = "Tên đăng nhập đã tồn tại";
                lbthongbao.ForeColor = Color.Red;
            }

        }

        void login()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.UserDBs
                           where k.UserName == txtUserName.Text.Trim() && k.Password == Global.ToEncoding(txtPass1.Text.Trim()) && k.visible == true
                           select k;

                foreach (UserDB u in list)
                {
                    Session[Global.SESS_USER] = u;
                }
                Session.Timeout = 120;
                string link = pathUrl + "/Trang-chu";
                if (Session[Global.SESS_USER] != null)
                    Response.Redirect(link);
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }
    }
}